package com.pda.common_easy.pages.main;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.pda.common_easy.R;
import com.pda.common_easy.beanout.LoginBeanOut;
import com.pda.common_easy.message.MessageWarp;
import com.public_module.tools.SharedPreferencesManger;

import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<LoginBeanOut.DataDTO.ShowInfoDTO> mlists = new ArrayList<>();

    public MainAdapter(Context context, List<LoginBeanOut.DataDTO.ShowInfoDTO> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_main, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        if(position%2==0){
//            viewHolder.rl.setBackgroundColor(mContext.getColor(R.color.white));
//        }else {
//            viewHolder.rl.setBackgroundColor(mContext.getColor(R.color.whitee));
//        }
        viewHolder.ttv.setText(mlists.get(position).getText());
        if(SharedPreferencesManger.getLoacl()){
            if(mlists.get(position).getId()==4){
                Glide.with(mContext).load(R.mipmap.icon_in).into(viewHolder.tiv);
            }else if(mlists.get(position).getId()==5){
                Glide.with(mContext).load(R.mipmap.icon_out).into(viewHolder.tiv);
            }else if(mlists.get(position).getId()==6){
                Glide.with(mContext).load(R.mipmap.icon_pro).into(viewHolder.tiv);
            }else if(mlists.get(position).getId()==10){
                Glide.with(mContext).load(R.mipmap.icon_about).into(viewHolder.tiv);
            } else if(mlists.get(position).getId()==11){
                Glide.with(mContext).load(R.mipmap.icon_exit).into(viewHolder.tiv);
            } else if(mlists.get(position).getId()==13){
                Glide.with(mContext).load(R.mipmap.icon_clear).into(viewHolder.tiv);
            }
        }else {
            Glide.with(mContext).load(mlists.get(position).getIcon()).into(viewHolder.tiv);
        }
        viewHolder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageWarp  messageWarp = new MessageWarp();
                messageWarp.setCode(mlists.get(position).getId());
                messageWarp.setBillType(mlists.get(position).getBillType());
                messageWarp.setText(mlists.get(position).getText());
                EventBus.getDefault().post(messageWarp);
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.tiv)
        ImageView tiv;
        @BindView(R.id.ttv)
        TextView ttv;
        @BindView(R.id.rl)
        LinearLayout rl;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

